var peyote = {};
var peyoteFilePath = rootPath;
var system_mode = 0;
var hostBrowser = 0;
var hostOS = 0;
var screen_mode = 0;
var loadLock = 0;

//Force  mode
//system_mode = "MOBILE";
system_mode = "DESKTOP";

flipLock = function()
{
 loadLock--;
};

syncLock = function()
{
    if(loadLock > 0)
        setTimeout(syncLock,1000);
    else 
        return;
};

loadJS = function(filename)
{
    loadLock++;
    var fileref = document.createElement("script");
    fileref.setAttribute("type","text/javascript");
    fileref.setAttribute("src", filename);
    
    fileref.onload = flipLock;
    
    document.getElementsByTagName("head")[0].appendChild(fileref);
};

execJS = function(filename)
{
    loadLock++;
    var fileref = document.createElement("script");
    fileref.setAttribute("type","text/javascript");
    fileref.setAttribute("src", filename);
    
    fileref.onload = flipLock;
    
    document.getElementsByTagName("head")[0].appendChild(fileref);
};


function detectSystem()
{
	//var system_mode = 0;
	//var hostBrowser = 0;
	//var hostOS = 0;
	//var screen_mode = 0;
	var useragent = navigator.userAgent;
	
	if( Boolean(useragent.match(/(Large Screen)|GoogleTV/i)) )
	{
		system_mode = "TV";
		return;
		
	}
	else if( Boolean(useragent.match(/iPhone/i)) )
	{
		system_mode = "MOBILE";
		
		return;
		
	}
	else if( Boolean(useragent.match(/iPad/i)) )
	{
		system_mode = "MOBILE";
		return;
	}
	else if( Boolean(useragent.match(/iPod/i)) )
	{
		system_mode = "MOBILE";
		return;
		
	}
	else
	{
		system_mode = "DESKTOP";
		return;		
	}
	
};

peyote.init = function(mode)
{
    //system_mode = mode;
    detectSystem();
	
    if(system_mode == "TV")
    {
        screen_mode == "PORTRAIT";
    }
    else if(system_mode == "MOBILE")
    {
        screen_mode == "LANDSCAPE";    
    }
    else if(system_mode == "DESKTOP")
    {
        screen_mode == "PORTRAIT";    
    }    
    else if(system_mode == "TERMINAL")
    {
        screen_mode == "PORTRAIT";    
    }    
    else if(system_mode == "AUTO")
    {
        screen_mode == "PORTRAIT";    
    }     
      
    loadJS(peyoteFilePath+"/corekit/BBase.js");
        
    peyote.appkit();
    peyote.gamekit();
    peyote.mediakit();
    peyote.storagekit();
	peyote.themekit();
    peyote.uikit();
    peyote.animationkit();    
};

peyote.run = function()
{
    if(loadLock == 0)
    {
        start(0,0);
    }
    else
    {
        setTimeout(peyote.run,1000);
    }
};

peyote.appkit = function()
{
    loadJS(peyoteFilePath+"/appkit/app.js");
};

peyote.gamekit = function()
{
    //loadJS("js/gamekit/game.js");
};

peyote.mediakit = function()
{
    loadJS(peyoteFilePath+"/mediakit/media.js");
};

peyote.storagekit = function()
{
    loadJS(peyoteFilePath+"/storagekit/storage.js");
};

peyote.themekit = function()
{
    loadJS(peyoteFilePath+"/themekit/theme.js");
};

peyote.uikit = function()
{
    loadJS(peyoteFilePath+"/uikit/ui.js");
    loadJS(peyoteFilePath+"/uikit/uiViews.js");	
    loadJS(peyoteFilePath+"/uikit/WindowKit.js");

    loadJS(peyoteFilePath+"/uikit/views/toolbar.js");    
    loadJS(peyoteFilePath+"/inputkit/input.js");    
};

peyote.animationkit = function()
{
    loadJS(peyoteFilePath+"/animationkit/animation.js");
};

peyote.setstyle = function(styleFile)
{

};